package com.hongxun.design.jungong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectAttributeCodeActivity;
import com.hongxun.design.fragments.BaseFragment;
import com.hongxun.design.jungong.activity.ChoiceModelActivity_jg;
import com.hongxun.design.jungong.activity.Mainactivity_jg;
import com.hongxun.design.jungong.activity.OldBjActivity;
import com.hongxun.design.jungong.model.RequestBeanNew;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.sqlite.dao.OldBjDao;
import com.hongxun.design.ui.SettingTypeActivity;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.pop.android.common.util.ToastUtils;

/**
 * 项目材料、典设、模块数量及其它操作
 */

public class FragmentMaterial1 extends BaseFragment implements View.OnClickListener {
    private static HeadSecondControlPanel headsecond_layout;
    OldBjDao oldBjDao;
    private View view;
    private TextView number1, number2, number3,bj_num;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HttpStringUtil.SUCCESSED:
                    if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                        RequestBeanNew aNew = FastJsonUtil.JsonToGetT((String) msg.obj, new TypeReference<RequestBeanNew>() {
                        });
                        String s = aNew.getAllcount();
                        if (s.contains("|")) {
                            String[] slit = s.split("\\|");
                            for (int i = 0; i < slit.length; i++) {
                                if (i == 0) number1.setText(slit[i]);
                                else if (i == 1) number2.setText(slit[i]);
                                else if (i == 2) number3.setText(slit[i]);
                            }
                        } else {
                            number1.setText("0");
                            number2.setText("0");
                            number3.setText("0");
                        }
                    } else {
                        showToast(FastJsonUtil.JsonToCheck_String(String.valueOf(msg.obj)));
                    }
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.jg_fragmnet_material1, container, false);
        findViewById();
        return view;
    }

    private void findViewById() {
        oldBjDao=new OldBjDao(getActivity());
        initheader();
        number1 = (TextView) view.findViewById(R.id.tv_number_material);
        bj_num = (TextView) view.findViewById(R.id.bj_num);
        TextView impMatreial = (TextView) view.findViewById(R.id.tv_go_material);
        TextView tv_bj_model = (TextView) view.findViewById(R.id.tv_bj_model);
        TextView updateSort = (TextView) view.findViewById(R.id.tv_update_sort);
        number2 = (TextView) view.findViewById(R.id.tv_number_mk);
        TextView impMK = (TextView) view.findViewById(R.id.tv_go_mk);
        number3 = (TextView) view.findViewById(R.id.tv_number_model);
        TextView impModel = (TextView) view.findViewById(R.id.tv_go_model);
        SetTypeface.set(impMatreial, IconSourse.ADD);
        SetTypeface.set(updateSort, IconSourse.EDIT);
        SetTypeface.set(impMK, IconSourse.EDIT);
        SetTypeface.set(impModel, IconSourse.EDIT);
        impMatreial.append(" 导入项目材料表");
        updateSort.append(" 材料分类排序");
        impMK.append(" 组装模块");
        impModel.append(" 组装典设");
        tv_bj_model.append("导入表计信息");
        tv_bj_model.setOnClickListener(this);
        impMatreial.setOnClickListener(this);
        impMK.setOnClickListener(this);
        impModel.setOnClickListener(this);
        updateSort.setOnClickListener(this);
    }

    private void getProMateialCount() {
        if (MyApp.NetWorkConnect) {
            String versionId = MyApp.currProject.getCodeset_version_id();
            if (versionId == null) versionId = "0";
            MyApp.HTTP.getProCount(handler, versionId, HttpStringUtil.SUCCESSED);
        } else showToast("暂无网络");
    }

    private void initheader() {
        headsecond_layout = (HeadSecondControlPanel) view.findViewById(R.id.headsecond_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(IconSourse.ADD + "",
                View.GONE, R.color.bar_right, ListenerSet.back, "", R.color.title,
                View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        headsecond_layout.setSearchTitle("");
        //切换项目
        headsecond_layout.setSearchOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(MString.TYPE, MString.projectChoice);
                IntentToActivity.intent(getActivity(), CreateProjectAttributeCodeActivity.class, bundle);
            }
        });

        String projectName = String.valueOf(MyApp.currProject.getProject_name()).trim();
        if (!projectName.contains("null") || !projectName.isEmpty())
            headsecond_layout.setSearchTitle(projectName);
    }

    private void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        String projectName = String.valueOf(headsecond_layout.stitle.getText()).trim();
        if (projectName.isEmpty()) {
            showToast("请先选择项目！");
            return;
        }
        switch (v.getId()) {
            case R.id.tv_go_material://导入材料
                Bundle bundle = new Bundle();
                bundle.putString(MString.CODE, "1");
                IntentToActivity.intent(getActivity(), Mainactivity_jg.class, bundle);
                break;
            case R.id.tv_go_mk://组装模块
                if (MyApp.activity == null) return;
                Bundle bundleMK = new Bundle();
                bundleMK.putString(MString.CODE, MString.Modular);
                bundleMK.putString(MString.project_name, MyApp.currProject.getProject_name());
                IntentToActivity.intent(MyApp.activity, ChoiceModelActivity_jg.class, bundleMK);
                break;
            case R.id.tv_go_model://组装典设
                if (MyApp.activity == null) return;
                Bundle bundleM = new Bundle();
                bundleM.putString(MString.CODE, MString.ChoiceModelData);
                bundleM.putString(MString.project_name, MyApp.currProject.getProject_name());
                IntentToActivity.intent(MyApp.activity, ChoiceModelActivity_jg.class, bundleM);
                break;
            case R.id.tv_update_sort:
                if(TextUtils.isEmpty(number1.getText().toString()) || number1.getText().toString().trim().equals("0")){
                    ToastUtils.showToast(getActivity(),"请先添加材料");
                    return;
                }
                Intent intent=new Intent(getActivity(), SettingTypeActivity.class);
                intent.putExtra("update",true);
                startActivity(intent);
                break;
            case R.id.tv_bj_model:
                Intent intent1=new Intent(getActivity(), OldBjActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getProMateialCount();
        if(MyApp.currProject!=null){
            long count=oldBjDao.getCount(MyApp.currProject.getProject_id());
            bj_num.setText(count+"");
        }else{
            bj_num.setText(0+"");
        }
        //设置项目名称
        String projectName = MyApp.currProject.getProject_name();
        String trim = String.valueOf(headsecond_layout.stitle.getText()).trim();
        if (projectName.length() == 0 || projectName.equals("null")) return;
        if (!projectName.equals(trim)) headsecond_layout.setSearchTitle(projectName);
    }


}
